package com.jzqf.compose.ui

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.jzqf.compose.R
import com.jzqf.compose.navigation.navigateTo
import com.jzqf.core.ui.theme.ColorCore


@Composable
fun BottomNavigation(navController: NavHostController) {
    NavigationBar(
        modifier = Modifier
            .fillMaxWidth()
            .height(60.dp)
    ) {
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentDestination = navBackStackEntry?.destination
        navigationItemBeanList.forEach { navigationItemBean ->
            val selected =
                currentDestination.isTopLevelDestinationInHierarchy(navigationItemBean.route)
            NavigationBarItem(selected = selected, onClick = {
                navController.navigateTo(routePath = navigationItemBean.route)
            }, icon = {
                Image(
                    painter = painterResource(id = if (selected) navigationItemBean.iconSelected else navigationItemBean.icon),
                    contentDescription = navigationItemBean.title,
                    modifier = Modifier.size(width = 20.dp, height = 20.dp)
                )
            }, label = {
                Text(
                    text = navigationItemBean.title,
                    color = if (selected) ColorCore.ColorPrimary else ColorCore.Color_2D3341
                )
            })
        }
    }
}

@Preview
@Composable
fun BottomNavigationPreview() {
    val navController = rememberNavController()
    BottomNavigation(navController)
}

private fun NavDestination?.isTopLevelDestinationInHierarchy(route: String) = this?.hierarchy?.any {
    it.route?.contains(route, true) ?: false
} ?: false

data class NavigationItemBean(
    val title: String,
    val icon: Int,
    val iconSelected: Int,
    val route: String,
)

val navigationItemBeanList = listOf(
    NavigationItemBean(
        title = "首页",
        icon = R.drawable.ic_home,
        iconSelected = R.drawable.ic_home_selected,
        route = "HomeScreen"
    ),
    NavigationItemBean(
        title = "商城",
        icon = R.drawable.ic_mall,
        iconSelected = R.drawable.ic_mall_selected,
        route = "MallScreen"
    ),
    NavigationItemBean(
        title = "博客",
        icon = R.drawable.ic_wan,
        iconSelected = R.drawable.ic_wan_selected,
        route = "WanScreen"
    ),
    NavigationItemBean(
        title = "我的",
        icon = R.drawable.ic_mine,
        iconSelected = R.drawable.ic_mine_selected,
        route = "MineScreen"
    ),
)